package dimensional.traveler.client.screens.welcome;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

import dimensional.traveler.client.UserManagement;
import dimensional.traveler.client.mvc.ControlledComposite;
import dimensional.traveler.client.mvc.MediatedController;

public class WelcomeScreen extends ControlledComposite implements ClickHandler {
	
	private static final int IMAGE_SHUFFLE_INTERVAL = 5000;
	
	private static WelcomeScreenUiBinder uiBinder = GWT.create(WelcomeScreenUiBinder.class);
	
	interface WelcomeScreenUiBinder extends UiBinder<Widget, WelcomeScreen> {
	}
	
	@UiField
	Image image;
	@UiField
	SpanElement namePanel;
	@UiField
	Button continueButton;
	@UiField
	Button newGameButton;
	@UiField
	Button loadGameButton;
	@UiField
	Button logoutButton;
	 
	private WelcomeScreenController controller = new WelcomeScreenController(); 
	private List<String> imagePaths = new LinkedList<String>();
	private Timer imageTimer;
	
	public WelcomeScreen() {
		initWidget(uiBinder.createAndBindUi(this));
		initNamePanelWithLoggedInUser();
		initButtons();
		setupController();
	}

	private void initNamePanelWithLoggedInUser() {
		namePanel.setInnerText(UserManagement.getLoggedInUser());
	}
	
	private void initButtons() {
		newGameButton.addClickHandler(this);
		continueButton.addClickHandler(this);
		continueButton.setEnabled(false);
		loadGameButton.addClickHandler(this);
		loadGameButton.setEnabled(false);
		logoutButton.addClickHandler(this);
	}
	
	private void setupController() {
		controller.setWelcomeScreen(this);
		this.addAttachHandler(controller);
	}
	
	void addImagePaths(Collection<String> imagePaths){
		this.imagePaths.addAll(imagePaths);
	}
	
	void startShufflingImages() {
		int length = imagePaths.size();
		if (length == 0)
			return;
		setupTimer(length);
		imageTimer.run();
		imageTimer.scheduleRepeating(IMAGE_SHUFFLE_INTERVAL);
	}
	
	private void setupTimer(final int numberOfImages) {
		imageTimer = new Timer() {
			@Override
			public void run() {
				int index = Random.nextInt(numberOfImages);
				WelcomeScreen.this.image.setUrl(imagePaths.get(index));
			}
		};
	}
	
	void stopShufflingImages() {
		if (imageTimer != null)
			imageTimer.cancel();
	}
	
	@Override
	public void onClick(ClickEvent event) {
		if (event.getSource() == logoutButton) {
			controller.logout();
		} else if (event.getSource() == newGameButton) {
			controller.newGame();	
		} else if (event.getSource() == continueButton){
			controller.continueGame();
		} else if (event.getSource() == loadGameButton){
			controller.loadGame();
		}
	}

	@Override
	public MediatedController getController() {
		return controller;
	}

}
